import $ajax from '../ajax'
import { baseUrl,adUrl,adConfig,channelUrl,fingerprintUrl,hlAdLogUrl,pkgChannel,appSource } from '../config'
import md5 from 'crypto-js/md5';
import { setGlobalData, getGlobalData } from '../../global'
/**
 * @desc 在实际开发中，您可以将 baseUrl 替换为您的请求地址前缀；
 *
 * 已将 $apis 挂载在 global，您可以通过如下方式，进行调用：
 * $apis.example.getApi().then().catch().finally()
 *
 */
const jisuKey = 'd1523b388cafe716'
export default {
  //协议
  getProtocol() {
    const data ={
      appSource:appSource,
      channel:pkgChannel
    }
    return $ajax.post(`${baseUrl}/config/getProtocolConfig`, data, {auth:false})
  },
  getMonthlyList(data){
    return $ajax.get(`${baseUrl}/user-account-books/monthlyList`, data)
  },
  booksCreate(data){
    return $ajax.post(`${baseUrl}/user-account-books/create`, data)
  },
  getUnit(data){
    return $ajax.get(`https://api.jisuapi.com/unitconvert/unit`,{...data,appkey:jisuKey})
  },
  getUnitConvert(data){
    return $ajax.get(`https://api.jisuapi.com/unitconvert/convert`,{...data,appkey:jisuKey})
  },
  getExchange(data){
    return $ajax.get(`https://api.jisuapi.com/exchange/currency`,{...data,appkey:jisuKey})
  },
  getExchangeConvert(data){
    return $ajax.get(`https://api.jisuapi.com/exchange/convert`,{...data,appkey:jisuKey})
  },
  delBill(data){
    return $ajax.post(`${baseUrl}/user-account-books/del/${data}`,{})
  },
  updateBill(data){
    return $ajax.post(`${baseUrl}/user-account-books/update`,data)
  },
  feedbackAdd(data){
    return $ajax.post(`${baseUrl}/feedback/add`, {...data,channel:pkgChannel})
  },
  userReportAdd(data){
    return $ajax.post(`${baseUrl}/userReport/add`, {...data,channel:pkgChannel})
  },
  getXhySearch(data){
    return $ajax.get(`https://api.jisuapi.com/xhy/search`,{...data,appkey:jisuKey})
  },
  getCmySearch(data){
    return $ajax.get(`https://api.jisuapi.com/miyu/search`,{...data,appkey:jisuKey})
  },

  // et15:每次拉起都上报后端 et16:应用打开上报后端
  addReported(et=15) {
    const time = Number(new Date())
    const { brand, osVersionCode, osVersionName, model } = getGlobalData('deviceInfo')
    const { mac, device, user } = getGlobalData('idInfo')
    let adchannel = getGlobalData('adchannel')
    const { od, odMd5, azem, azemMd5 } = this.getDeviceObj(osVersionName,device)
    const params = {
      et:et,//应用拉起
      union_site: adchannel?.aidReportChannel || '',//归因渠道---可选

      app_source: appSource,//app来源
      channel: pkgChannel,//渠道
      appver: '', //app 版本
      brand:brand,//品牌
      os: 0,//操作系统
      osver: osVersionCode,//操作系统版本号
      azem: azemMd5 ? azemMd5 : '',//imei mad5值---可选
      od: odMd5 ? odMd5 : '',//oaid md5值
      azidm: md5(user),//androidld md5值---可选
      did: getGlobalData('androidId'),//设备deviceid
      mcm: mac ? mac.split(':').join('') : '',//设备mac地址
      wz:'',
      pig:"",
      ml: model,//设备型号
      network:"",
      carrier:"",
      ts: time,//数据上报时间
      uua:"",
      uid:"",
      fingerprint: getGlobalData('fingerprint'),//设备指纹
      aid: adchannel?.aid || '',
      adv_id: adchannel?.advId || '',
      ca_id: adchannel?.caId || '',
      cid: adchannel?.cid || '',
      agency:adchannel?.agency||'',
    }
    return $ajax.get(`${adUrl}`, params, { tag: 'ad' })
  },
  //上报接口
  adReport(data){
    const time = Number(new Date())
    const {brand,osVersionCode,osVersionName,model} = getGlobalData('deviceInfo');
    const {mac,device,user} = getGlobalData('idInfo');
    let adchannel = getGlobalData('adchannel')
    const {od,odMd5,azem,azemMd5} = this.getDeviceObj(osVersionName,device);
    const params={
      app_source:appSource,
      did:getGlobalData('androidId'),
      fingerprint:getGlobalData('fingerprint'),
      channel:pkgChannel,
      appver:'',//app 版本
      brand:brand,
      os:0,
      osver:osVersionCode,
      mcm:mac?mac.split(':').join(''):'',
      ts:time,
      //azem加密、od加密
      azem:azemMd5?azemMd5:'',
      od:odMd5?odMd5:'',
      azidm:md5(user),
      ml:model,

      // 广告请求记录
      et:10,
      bt_id:'',
      report_type:'',
      bt_pname:'',
      bt_pid:'',
      source:'',
      wz:'',
      union_site:adchannel?.aidReportChannel||'',
      aid:adchannel?.aid||'',
      adv_id:adchannel?.advId||'',
      ca_id:adchannel?.caId||'',
      cid:adchannel?.cid||'',
      agency:adchannel?.agency||'',
      ...data
    }
    return $ajax.get(`${adUrl}`, params,{tag:'ad'})
  },
  //指纹
  getFingerprint(data){
    const {versionCode,user,mac,device,brand,model,osVersionName} = data
    const {od,odMd5,azem,azemMd5} = this.getDeviceObj(osVersionName,device)
    const params={
      appSource:appSource,
      channel:pkgChannel,
      appver:versionCode,
      os:0,
      //azem加密、od加密
      azem:azemMd5?azemMd5:'',
      od:odMd5?odMd5:'',
      azidm:md5(user),
      mcm:mac?mac.split(':').join(''):'',
      brand:brand,
      ml:model
    }
    return $ajax.get(`${fingerprintUrl}/device/fingerprint`, params,{auth:false})
  },
  //渠道标识
  getChannelV1(data){
    const {versionCode,user,mac,device,fingerprint,brand,pkg,osVersionName} = data
    const {od,odMd5,azem,azemMd5} = this.getDeviceObj(osVersionName,device)
    console.log('getChannelV1-mutual',getGlobalData('mutual'))
    const params={
      et:4,
      channel:pkgChannel,
      appSource:appSource,
      appver:versionCode,
      os:0,
      //azem加密、od加密、orgOd原值
      azem:azemMd5?azemMd5:'',
      od:odMd5?odMd5:'',
      orgOd:od?od:'',
      azidm:md5(user),
      mc:mac?mac.split(':').join(''):'',
      brand:brand,
      fingerprint:fingerprint,
      pkg:pkg,
      rType:getGlobalData('mutual')==1?'la':''
    }
    return $ajax.get(`${channelUrl}/launch/channelV1`, params,{auth:false})
  },
  //渠道标识（hap-标签匹配)
  getLaunchHap(data){
    const time = Number(new Date())
    const {versionCode,user,mac,device,fingerprint,brand,model,pkg,osVersionName} = data
    const {od,odMd5,azem,azemMd5} = this.getDeviceObj(osVersionName,device)
    const publicParams = getGlobalData('publicParams')
    console.log('getLaunchHap-mutual',getGlobalData('mutual'))
    const adPlatform = publicParams?.ad_platform||''
    let advId = adPlatform=='52'?publicParams?.corp_id:publicParams?.adv_id;
    let aid = adPlatform=='52'?publicParams?.campaign_id:publicParams?.aid
    let aidName = adPlatform=='52'?publicParams?.campaign_name:publicParams?.aid_name
    let cid = adPlatform=='52'?publicParams?.content_id:publicParams?.cid
    let requestId = adPlatform=='52'?publicParams?.transunique_id:publicParams?.request_id
    
    const params={
      appSource:appSource,
      appver:versionCode,
      channel:pkgChannel,
      fingerprint:fingerprint,
      pkg:pkg,
      brand:brand,
      mc:mac?mac.split(':').join(''):'',
      ts:time,
      //公参
      wap:publicParams?.wap||'',
      caid1:publicParams?.caid1||'',
      adPlatform:adPlatform,
      unionSite:publicParams?.channel||'',
      advId:advId||'',
      aidName:aidName||'',
      aid:aid||'',
      cid:cid||'',
      ml:model,
      imei:azem||publicParams?.imei||'',
      imeiMd5:azemMd5||publicParams?.imei_sum||'',
      ip:publicParams?.ip||'',
      requestId:requestId||'',
      oaid:od||publicParams?.oaid||'',
      oaidMd5:odMd5||publicParams?.oaid_sum||'',
      uua:publicParams?.uua||'',
      deviceId:device||publicParams?.device_id||'',
      deviceIdMd5:md5(device)||publicParams?.device_id_sum||'',
      callback:publicParams?.callback||'',
      callbackParam:publicParams?.cb_par||'',
      clickId:publicParams?.click_id||'',
      caId:publicParams?.adgroup_id||'',

      dpLink:'',//暂时传空
      rType:getGlobalData('mutual')==1?'la':''
    }
    return $ajax.get(`${channelUrl}/launch/hap`, params,{auth:false})
  },
  //转化事件
  gethlAdLog(data){
    const {device,user,mac} = getGlobalData('idInfo')
    const {brand,model,osVersionName} = getGlobalData('deviceInfo')
    const fingerprint = getGlobalData('fingerprint')
    const {event,versionCode} = data
    const {od,odMd5,azem,azemMd5} = this.getDeviceObj(osVersionName,device)
    const params={
      appSource:appSource,
      appver:versionCode,
      channel:pkgChannel,
      // od原值、azem 加密
      od:od?od:'',
      azem:azemMd5?azemMd5:'',
      azid:md5(user),
      pig:'',
      os:0,
      uua:'',
      event:event,
      ts:Number(new Date()),
      ml:brand+model,
      mc:mac?mac.split(':').join(''):'',
      muid:'',
      uid:'',
      blackBox:'',
      appType:'',
      fingerprint:fingerprint,
      atUuid:'',
      sign:'',
    }
    return $ajax.get(`${hlAdLogUrl}/hlAdLog/report`, params,{auth:false})
  },
  //广告配置
  getAdConfig(data){
    const {versionCode,user,mac,device,fingerprint,osVersionName,aidReportChannel} = data
    const {od,odMd5,azem,azemMd5} = this.getDeviceObj(osVersionName,device)
    const params={
      channel:pkgChannel,
      appSource:appSource,
      appVersion:versionCode,
      deviceId:device,
      fingerprint:fingerprint,
      // imei加密、oaid 加密
      imei:azemMd5?azemMd5:'',
      oaid:odMd5?odMd5:'',
      androdid:user,
      mac:mac,
      os:0,
      aidReportChannel:aidReportChannel
    }
    return $ajax.get(`${adConfig}/v1/luck/load`, params,{auth:false})
  },
  getDeviceObj(osVersionName,device){
    let deviceObj={}
    let oaidObj = getGlobalData('oaid');
    let oaid = oaidObj?.oaid;
    if(oaid&&oaid!='') {
      deviceObj={
        od:oaid,
        odMd5:md5(oaid)
      }
    }else{
      // 取device 安卓版本>=10传od 否则传azem
      if(osVersionName>=10){
        deviceObj={
          od:device,
          odMd5:device!=''?md5(device):device
        }
      }else{
        deviceObj={
          azem:device,
          azemMd5:device!=''?md5(device):device
        }
      }
    }
    return deviceObj
  }
}
